import axios from "axios";
import service from "@/config/index";

function getTableColumns(data) {
  let url = "/cp/user/menu/table";
  return axios.post(url, data);
}

function getTableFilters(data = {}) {
  let url = "/cp/user/menu/search";
  return axios.post(url, data);
}

export default {
  async getTableConfig(name, description) {
    let url = "/cp/menu/config/business/getMenu";
    let response = await axios.post(url, { menuName: description, name });
    let data = {
      display: true,
      menuId: response.data,
      sortSort: "ASC",
      valid: true
    };
    let [filters, columns] = await Promise.all([
      getTableColumns(data),
      getTableFilters(data)
    ]);
    return {
      filters: filters.data,
      columns: columns.data,
      code: filters.code || columns.code
    };
  },
  async load(menuId) {
    let data = {
      menuId,
      display: true,
      sortSort: "ASC",
      valid: true
    };
    let [filters, columns] = await Promise.all([
      getTableColumns(data),
      getTableFilters(data)
    ]);
    return {
      filters: filters.data,
      columns: columns.data,
      code: filters.code || columns.code
    };
  },
  search(url, filter) {
    return axios.post(url, filter);
  },
  submitFormContainer(url, params, type) {
    return axios[type ? "put" : "post"](url, params);
  },
  tableSum(data) {
    let url = `${service.serviceOrder}/search/sum`;
    return axios.post(url, data);
  }
};
